﻿#region File Description
///------------------------------------------------------------------
/// Program		  :	Algorithm With Graph
/// Written by	  :	Nguyen Khac Trung, Le Hoang Tuan
/// Email		  :	nkt1321991@gmail.com || hoangtuanuit@gmail.com
/// File Name     : MyItemList.cs
/// Created date  :	10/11/2011
/// Modified by	  :	Nguyen Khac Trung
/// Modified date :	11/11/2011
/// Version		  :	1.2
/// Description	  :	Declare my class Point
///------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
#endregion


namespace Graph
{
    /// <summary>
    /// 
    /// </summary>
    public class MyItemList
    {

        #region Fields

        private List<String> m_listMyItem;

        private MyPointList m_listMyPointInListItem;

        private MyPathList m_listMyPathInListItem;

        private Canvas m_panelViewMain;

        private Canvas m_panelViewMain1;

        private IInputElement m_iInputElement;

        #endregion

        #region Properties

        public List<String> LISTMYITEM
        {
            get { return m_listMyItem; }
            set { m_listMyItem = value; }
        }

        public MyPointList LISTMYPOINTINLISTITEM
        {
            get { return m_listMyPointInListItem; }
            set { m_listMyPointInListItem = value; }
        }

        public MyPathList LISTMYPATHINLISTITEM
        {
            get { return m_listMyPathInListItem; }
            set { m_listMyPathInListItem = value; }
        }

        public Canvas PANELVIEWMAIN
        {
            get { return m_panelViewMain; }
            set { m_panelViewMain = value; }
        }

        public Canvas PANELVIEWMAIN1
        {
            get { return m_panelViewMain; }
            set { m_panelViewMain = value; }
        }

        #endregion

        #region Initialization

        public MyItemList(Canvas _panelViewMain, Canvas _panelViewMain1, IInputElement _iInPutElement)
        {
            m_panelViewMain = _panelViewMain;
            m_panelViewMain1 = _panelViewMain1;
            m_iInputElement = _iInPutElement;
            m_listMyItem = new List<String>();
            m_listMyPointInListItem = new MyPointList(m_iInputElement);
            m_listMyPathInListItem = new MyPathList(m_listMyPointInListItem, m_iInputElement);
        }

        #endregion

        #region Process

        public void AddPointToMyList(bool? _isCheckedBoxNamePoint)
        {
            MyPointGeometry point = m_listMyPointInListItem.AddPointToPointList(_isCheckedBoxNamePoint);
            if (point != null)
            {
                Image image = new Image();
                image.BeginInit();
                image.Source = new BitmapImage(new Uri("Images/" + m_listMyPointInListItem.LISTMYPOINT[m_listMyPointInListItem.LISTMYPOINT.Count - 1].ELL.Name
                                                        + ".png",UriKind.Relative));
                image.EndInit();
                image.Margin = new System.Windows.Thickness(m_listMyPointInListItem.LISTMYPOINT[m_listMyPointInListItem.LISTMYPOINT.Count - 1].ELL.X - 12.5,
                                                            m_listMyPointInListItem.LISTMYPOINT[m_listMyPointInListItem.LISTMYPOINT.Count - 1].ELL.Y- 12.5, 0, 0);
                image.IsHitTestVisible = false;
                m_panelViewMain.Children.Add(point);
                m_panelViewMain1.Children.Add(image);
                m_listMyItem.Add(m_listMyPointInListItem.LISTMYPOINT[m_listMyPointInListItem.LISTMYPOINT.Count - 1].ELL.Name);
            }
        }

        public void AddLineToMyList()
        {
            Path path = m_listMyPathInListItem.AddMyLineToMyList();
            if (path != null)
            {
                m_panelViewMain.Children.Add(path);
                m_listMyItem.Add(path.Name);
            }
        }

        public void AddCircleToMyList()
        {
            Path path = m_listMyPathInListItem.AddMyCircleToMyList();
            if (path != null)
            {
                m_panelViewMain.Children.Add(path);
                m_listMyItem.Add(path.Name);
            }
        }

        public void RemovePathWhenRemovePoint(String _str)
        {
            int index = 0;
            List<int> tempList = new List<int>();
            foreach (MyPath path in m_listMyPathInListItem.LISTMYPATH)
            {
                if (path.NAMEFIRSTPOINT == _str || path.NAMEENDPOINT == _str)
                {
                    m_panelViewMain.Children.RemoveAt(SearchIndex(path.MYPATH.Name));
                    m_listMyItem.RemoveAt(SearchIndex(path.MYPATH.Name));
                    tempList.Add(index);
                }
                ++index;
            }

            if (tempList.Count != 0)
            {
                int i = tempList.Count;
                while (i != 0)
                {
                    m_listMyPathInListItem.LISTMYPATH.RemoveAt(tempList[i - 1]);
                    --i;
                }
            }
        }

        public void RemovePointToMyList()
        {
            String str = m_listMyPointInListItem.RemovePointToPointList();
            if (str != null)
            {
                int index = SearchIndex(str);
                m_panelViewMain.Children.RemoveAt(index);
                index = m_listMyPointInListItem.SearchIndex(str);
                m_panelViewMain1.Children.RemoveAt(index + 1);
                m_listMyPointInListItem.LISTMYPOINT.RemoveAt(index);
                m_listMyItem.RemoveAt(index);
                RemovePathWhenRemovePoint(str);
            }
        }

        public int SearchIndex(String _str)
        {
            int index = 0;
            foreach (String temp in m_listMyItem)
            {
                if (temp == _str)
                {
                    return index;
                }
                ++index;
            }

            return index;
        }

        #endregion

    }
}
